package com.wits100.demoplayer;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.wits.util.ImageCache;

import java.io.IOException;

public class ImageActivity extends AppCompatActivity {


    private SimpleImageCache    imageCache = new SimpleImageCache();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        imageCache.open(imageCache.getCachePath());

        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        if (url != null) {
            showImage(url);
        }
    }


    protected void showImage(String url) {
        final ImageView imageView = (ImageView)findViewById(R.id.imageView);

        imageCache.getImage(url, new ImageCache.OnImageReady() {
            @Override
            public void handle(String uri, Uri localUri) {
                if (localUri != null) {
                    try {
                        Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), localUri);
                        imageView.setImageBitmap(bmp);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }


}
